﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using VitCode.ObjectResolutionHelper;

namespace VitCode.ObjectSettingApp
{
    public class ClassRoleChecker
    {

        public void CheckClass(Type checkObj,string dbKeyProName,Type dbKeyProType)
        {
            var dbKeyPro = checkObj.GetProperty(dbKeyProName);
            if (dbKeyPro == null)
            {
                throw new Exception("框架托管对象["+checkObj.Name+"]必须包含属性[" + dbKeyProName + "]");
            }
            if (dbKeyPro.PropertyType != dbKeyProType)
            {
                throw new Exception("历史对象的[" + dbKeyProName + "]属性必须为["+dbKeyProType.Name+"]类型,当前是[" + dbKeyPro.PropertyType + "]类型");
            }
        }

        public void CheckClass(Type objType)
        {
            
            if (objType.Name.EndsWith(HistoryKeyInfo.LAST_STRING))
                CheckClass(objType, HistoryKeyInfo.H_ID, typeof(Guid));
            else if (objType.Name == HistoryKeyInfo.HIS_VERSION_INFO)
                CheckClass(objType, HistoryKeyInfo.ID, typeof(Int32));
            else if(!HistoryKeyInfo.WithOutClassNames.Contains(objType.Name))
                CheckClass(objType, BaseDataKeyInfo.ID, typeof (Guid));
            
            var ps = objType.GetProperties();
            var scProMemo = new StringCollection();
            var errMsg = new StringBuilder();
            foreach(var p in ps)
            {
                var strDes = LookUpClassHelper.GetOrMemoDesValue(p);
                if (scProMemo.Contains(strDes))
                    errMsg.Append("类[" + objType.Namespace + "." + objType.Name + "]存在重复的属性描述ProName:["+p.Name+"] Des:["+strDes+"]\r\n");
                else
                    scProMemo.Add(strDes);
            }
            if (errMsg.Length > 0)
                throw new Exception(errMsg.ToString());
        }
    }
}
